package io.github.talelin.latticy.controller.v1;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.Logger;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.bo.ActivityBO;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.model.Activity;
import io.github.talelin.latticy.service.ActivityService;
import io.github.talelin.latticy.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;

/**
 * activity控制器
 *
 * @author YiHua
 * @date 2021/11/30 3:04 下午
 */
@Validated
@RestController
@RequestMapping("/v1/activity")
@PermissionModule(value = "活动")
public class ActivityController {

     @Autowired
     private ActivityService activityService;         // 活动业务层

     /**
      * 创建一个活动
      *
      * @param activityBO 活动的入参信息
      */
     @GroupRequired
     @PermissionMeta(value = "创建活动")
     @PostMapping("/create")
     public CreatedVO create(@RequestBody @Validated ActivityBO activityBO) {
          this.activityService.create(activityBO);
          return new CreatedVO();
     }

     /**
      * 删除活动
      *
      * @param id activityId
      */
     @GroupRequired
     @PermissionMeta(value = "删除活动")
     @DeleteMapping("/{id}")
     public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
          Activity activity = this.activityService.getById(id);
          if (activity == null) {
               throw new NotFoundException("未找到相关活动", 90000);
          }
          this.activityService.getBaseMapper().deleteById(id);
          return new DeletedVO();
     }

     /**
      * 更新活动信息
      *
      * @param activityBO 活动的入参信息
      * @param id         activityId
      */
     @GroupRequired
     @PermissionMeta(value = "更新活动")
     @PutMapping("/{id}")
     public UpdatedVO update(@RequestBody @Validated ActivityBO activityBO,
                             @PathVariable @Positive(message = "{id.positive}") Integer id
     ) {
          activityService.update(activityBO, id);
          return new UpdatedVO();
     }

     /**
      * 查询活动的详细信息
      *
      * @param id activityId
      */
     @Logger(template = "{user.username}，操作了'查询活动的详细信息，活动的title为：{activity.title}")
     @GroupRequired
     @PermissionMeta(value = "查询活动的详细信息")
     @GetMapping("/{id}/detail")
     public ActivityDetailVO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
          return activityService.getDetailById(id);
     }

     /**
      * 查询多个活动,支持分页
      *
      * @param pageNum  false 当前页
      * @param pageSize false 每页条数
      */
     @GroupRequired
     @PermissionMeta(value = "查询多个活动简单信息")
     @GetMapping("/page")
     public PageResponseVO<Activity> page(
         @RequestParam(name = "pageSize", required = false, defaultValue = "10")
         @Min(value = 1, message = "{page.pageSize.min}")
         @Max(value = 30, message = "{page.pageSize.max}") Integer pageSize,
         @RequestParam(name = "pageNum", required = false, defaultValue = "0")
         @Min(value = 0, message = "{page.pageNum.min}") Integer pageNum
     ) {
          // 查询分页后的活动信息
          IPage<Activity> paging = this.activityService.paging(pageNum, pageSize);
          // 返回分页对象
          return PageUtil.build(paging);
     }
}
